import { Toast } from 'antd-mobile'
import { processColor } from 'react-native'
import update from 'immutability-helper'
import { createAction, NavigationActions } from '../../utils/index'
import * as rateAnalyzeService from '../../services/home/rateAnalyzeService'
import * as Libs from '../../utils/Libs'

const initState = {
  dqczDetail: [], // 短期偿债能力
  cqczDetail: [], //长期偿债能力
  yyDetail: [], //营运能力
  ylDetail: [], //盈利能力
  duBangData: {}, // 杜邦分析
}


export default {
  namespace: 'rateanalyze',
  state: initState,
  reducers: {
    changeState(state, { payload }) {
      return { ...state, ...payload}
    }
  },
  effects: {
    *test({ payload }, { call, select, put}) {
    },
    *getDuBangData( {payload}, {call, select, put}) {
      let params = {}
      console.log('payload is ', payload)
      params.startTime = (payload.startTime || "").replace('.', '-')
      params.endTime = (payload.endTime || "").replace('.', '-')
      const resp = yield call(rateAnalyzeService.getBuBang, params)
      if (resp.resultCode !== 200) {
        Toast.info(resp.msg, 1)
        return false
      } else {
        yield put(createAction("changeState")({
          duBangData: resp.data
        }))
      }
    },
    *getRateDetailMsg({payload}, { call, select, put}) {
      let params = {}
      params.startTime = (payload.startTime || "").replace('.','-')
      params.endTime = (payload.endTime || "").replace('.','-')
      const resp = yield call(rateAnalyzeService.getDetailMsg, params)
      if (resp.resultCode !== 200) {
        Toast.info(resp.msg, 1)
        return false
      }
      const respD = resp.data || []

      const dqcznl = respD.dqcznl || {}
      const dqcznlXq = dqcznl.xq || []
      const dqcznlRespD = rateAnalyzeService.buildD("短期偿债能力", "", dqcznlXq)

      const cqcznl = respD.cqcznl || {}
      const cqcznlXq = cqcznl.xq || []
      const cqcznlRespD = rateAnalyzeService.buildD("长期偿债能力", "", cqcznlXq)

      const yynl = respD.yynl || {}
      const yynlXq = yynl.xq || []
      const yynlRespD = rateAnalyzeService.buildD("营运能力", "", yynlXq)

      const ylnl = respD.ylnl || {}
      const ylnlXq = ylnl.xq || []
      const ylnlRespD = rateAnalyzeService.buildD("盈利能力", "", ylnlXq)

      yield put(createAction("changeState")({
        dqczDetail: [dqcznlRespD],
        cqczDetail: [cqcznlRespD],
        yyDetail: [yynlRespD],
        ylDetail: [ylnlRespD]
      }))
    }
  },
  subscriptions: {
  },
}
